<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/4/10
 * Time: 9:37
 */

namespace app\index\controller;

use GuzzleHttp\Client;

//require_once '../../../vendor/autoload.php';

class Api
{
//    protected $baseUrl = Config('IMG')  ;
    /** @var \GuzzleHttp\Client */
    protected $client;

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => Config('baseUrl'),
            'headers' => [
                'x-consumer-id' => '9f8e7f8a-f3de-4a05-9f02-bb1170f8fa6f'
            ],
        ]);
    }

    public function subrequests($requests)
    {
        foreach ($requests as &$request) {
            if (!isset($request['headers'])) {
                $request['headers'] = [];
            }
            $request['headers']['x-consumer-id'] = '9f8e7f8a-f3de-4a05-9f02-bb1170f8fa6f';
        }
        $response = $this->client->request('post', 'subrequests?_format=json', [
            'json' => $requests
        ]);
        return \GuzzleHttp\json_decode($response->getBody(), true);
    }

    public function get_menu($gid)
    {  //显示所有的信息
        if (in_array($gid, ['departments'])) { //科室筛选字段
            $response = $this->client->request('GET', "api/$gid?page[limit]=1000&sort=weight&fields[department]=name,weight,parent,description,image&include=image&fields[file--file]=url");
        } else if (in_array($gid, ['columns'])) { //栏目筛选字段
            $response = $this->client->request('GET', "api/$gid?page[limit]=1000&sort=weight");
        } else if (in_array($gid, ['doctors'])) { //医生筛选字段
            $response = $this->client->request('GET', "api/$gid?page[limit]=1000&sort=-weight&fields[doctor]=name,job_position,weigh,brief,department,uuid");
        } else if (in_array($gid, ['platforms'])) {
            $response = $this->client->request('GET', "api/$gid?page[limit]=1000&include=image, cover_image");
        } else {
            $response = $this->client->request('GET', "api/$gid?page[limit]=1000");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        }
    }

    public function get_detail($type, $cid, $promote)
    {  //显示某个模块下某ID的信息
        if ($promote) {
            if ($type == 'departments') {
                $response = $this->client->request('GET', "api/$type?filter[uuid][value]=$cid&filter[promote][value]=$promote&include=image&fields[department]=uuid,name,description.value,image&fields[file--file]=url");
            } else if ($type == 'doctors') {
                $response = $this->client->request('GET', "api/$type?filter[uuid][value]=$cid&filter[promote][value]=$promote&include=image&fields[doctor]=name,job_position,weigh,brief,department,uuid,image&fields[file--file]=url");
            } else if ($type == 'platforms') {
                $response = $this->client->request('GET',"api/$type?filter[uuid][value]=$cid");
            } else {
                $response = $this->client->request('GET', "api/$type?filter[uuid][value]=$cid&filter[promote][value]=$promote&include=image");
            }
        } else {
            $response = $this->client->request('GET', "api/$type?filter[uuid][value]=$cid&include=image");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        }
    }

    public function get_p_list($gid, $page, $promote)
    {  //显示某个模块下的指定条目数
        if ($promote) {
            $response = $this->client->request('GET', "api/$gid/?page[limit]=$page&filter[promote][value]=$promote&include=image&sort=-changed");
        } else {
            $response = $this->client->request('GET', "api/$gid/?page[limit]=$page&include=image&sort=-changed");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_index_doctors_list($gid, $promote)
    {
        $response = $this->client->request('GET', "api/doctors?fields[doctor]=name,brief,job_title,job_position,image,weight&sort=-weight&filter[promote][value]=$promote&include=image&fields[file--file]=url");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_sub_articlelist($gid, $cateId, $page, $promote)
    { //显示某个文章类目下的指定条目数

        if ($promote) {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$cateId&filter[status][value]=1&filter[promote][value]=$promote
&page[limit]=$page&include=image&fields[node--article]=uuid,title,external_url,created,image,sticky,body.summary,field_platform&sort=-sticky,-created&fields[file--file]=url");
        } else {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$cateId&filter[status][value]=1
&page[limit]=$page&include=image&fields[node--article]=uuid,external_url,title,created,image,sticky,body.summary,field_platform&sort=-sticky,-created&fields[file--file]=url");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_c_d_article($column, $departments, $promote, $page)
    { //获取指定栏目指定科室的文章
        if ($promote) {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$column&filter[department.uuid][value]=$departments&filter[status][value]=1&filter[promote][value]=$promote
&page[limit]=$page&include=image&fields[node--article]=uuid,title,created,external_url,image,sticky,body.summary&sort=-sticky,-created&fields[file--file]=url");
        } else {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$column&filter[department.uuid][value]=$departments&filter[status][value]=1&page[limit]=$page&include=image&fields[node--article]=uuid,external_url,title,created,image,sticky,body.summary&sort=-sticky,-created&fields[file--file]=url");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_p_detail($cid)//获取文章的详情
    {
        $response = $this->client->request('GET', "api/node/article?filter[uuid][value]=$cid&include=image&fields[node--article]=uuid,title,created,external_url,image,body&fields[file--file]=url");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_d_d_detail($cid)
    { //获取某个科室下的医生
        $response = $this->client->request('GET', "api/doctors?filter[department.uuid][value]=$cid&fields[doctor]=name,brief,body,job_title,job_position,image&sort=-weight&include=image&fields[file--file]=url");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_n_p_detail($cid)
    { //获取基本页面
        $response = $this->client->request('GET', "api/node/page/$cid");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);

        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function search($key, $page = 0, $size = 20)
    {  //查询
        $response = $this->client->request('GET', "api/search/node/$key?page=$page&size=$size&sort=-nid");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);

        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_leader_detail($id)
    {  //领导库领导信息 首页展示
        $response = $this->client->request('GET', "api/doctors?filter[department.uuid][value]=d3e91d2c-2054-4432-9369-1f46b905ba91&filter[uuid][value]=$id&fields[doctor]=name,body,brief,job_title,job_position,image&sort=-weight&include=image&fields[file--file]=url");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_sub_article($cateId)
    { //不显示列表直接显示栏目对应文章
        $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$cateId&filter[status][value]=1&fields[node--article]=uuid,title,created,body");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_slides($type)
    {
        $response = $this->client->request('GET', "api/slides?filter[status][value]=1&filter[category.uuid][value]=$type&fields[slides]=title,external_url,weight,category,field_type,image&sort=-weight&include=image&fields[file--file]=url&_consumer_id=9f8e7f8a-f3de-4a05-9f02-bb1170f8fa6f");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_platform_doctor($pid)
    { //获取某个平台下的医生
        $response = $this->client->request('GET', "api/doctors?filter[platform.uuid][value]=$pid&fields[doctor]=name,brief,body,job_title,job_position,image&sort=-weight&include=image&fields[file--file]=url");
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

    public function get_platform_article($pid, $type, $promote, $page)
    {//获取指定平台指定类型的文章
        switch($type) {
            case '科研动态': $column = array_search('科研动态',COLUMNS); break;
            case '科研成果': $column = array_search('科研成果',COLUMNS); break;
            default: return null; break;
        }
        if ($promote) {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$column&filter[platform.uuid][value]=$pid&filter[status][value]=1&filter[promote][value]=$promote
&page[limit]=$page&include=image&fields[node--article]=uuid,title,created,external_url,image,sticky,body.summary&sort=-sticky,-created&fields[file--file]=url");
        } else {
            $response = $this->client->request('GET', "api/node/article?filter[column.uuid][value]=$column&filter[platform.uuid][value]=$pid&filter[status][value]=1&page[limit]=$page&include=image&fields[node--article]=uuid,external_url,title,created,image,sticky,body.summary&sort=-sticky,-created&fields[file--file]=url");
        }
        $response = $response->getBody(true);
        if (!empty($response)) {
            return json_decode($response, true);
        } else {
            return array(msg, "不存在此信息");
        }
    }

}
